package com.ojoin.trade.supplyloan_core.web.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.dao.impl.SQLHelper;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;
import com.ojoin.trade.supplyloan_core.web.service.TotalService;

@Service
public class TotalServiceImpl implements TotalService {
	@Autowired
	private SQLHelper helper;

	@Autowired
	private TbSlPlatformUserService platformUserService;

	@Override
	public List<List<String>> getTotalCheck(String sql) {
		return helper.query(sql);
	}

	public List<String> getHead(int joinid) {
		List<String> li = new ArrayList<String>();
		li.add("日期");
		List<TbSlPlatformUser> users = platformUserService
				.queryTbSlPlatformUserByJoinid(joinid);

		if (!CommonUtils.isEmpty(users)) {
			Iterator<TbSlPlatformUser> it = users.iterator();
			while (it.hasNext()) {
				TbSlPlatformUser user = it.next();
				li.add(user.getPlatformName() + "库存");
				li.add(user.getPlatformName() + "结算");
				li.add(user.getPlatformName() + "付款");

			}
		}

		li.add("TOTAL");
		return li ;
	}

	@Override
	public String getSqlByMerchatId(int joinid) {

		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ifnull(date,'总计') as '日期',");
		Set set = new HashSet();

		List<TbSlPlatformUser> users = platformUserService
				.queryTbSlPlatformUserByJoinid(joinid);

		if (!CommonUtils.isEmpty(users)) {
			Iterator<TbSlPlatformUser> it = users.iterator();
			while (it.hasNext()) {
				TbSlPlatformUser user = it.next();
				set.add(user.getId());
				sb.append("sum(if(platform_account_id=" + user.getId()
						+ ",inventory,0)) as '" + user.getPlatformName()
						+ "库存',");
				sb.append("sum(if(platform_account_id=" + user.getId()
						+ " and statues='WaitPay' ,payment,0)) as '"
						+ user.getPlatformName() + "结算',");
				sb.append("sum(if(platform_account_id=" + user.getId()
						+ " and statues='CompletePay' ,payment,0)) as '"
						+ user.getPlatformName() + "付款',");
			}
		}

		sb.append("SUM(payment) AS TOTAL");
		String ids = set.toString().replace("[", "").replace("]", "");
		sb.append(" from (");
		sb.append("SELECT left(store_date,7) as date,platform_account_id,total_cost as inventory,0 as payment ,item_status as  statues from tb_sl_supply_inventory WHERE platform_account_id in("
				+ ids + ")");
		sb.append("union ALL ");
		sb.append("SELECT left(create_date,7) as date,platform_account_id,0 as inventory,amount as payment,settlement_state as statues  from tb_sl_supply_payment WHERE platform_account_id in("
				+ ids + ")");
		sb.append(")as t group by   date WITH ROLLUP;");

		return sb.toString();
	}

	public static void main(String[] args) {
		Set<Integer> se = new HashSet<>();
		se.add(12);
		se.add(34);
		se.add(45);

	}
}
